package com.kaisa.activity.ui;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ImageView;

import com.alibaba.fastjson.JSON;
import com.kaisa.activity.BaseApp;
import com.kaisa.activity.R;
import com.kaisa.activity.adapter.MoreThemeAdapter;
import com.kaisa.activity.bean.HomeEntity;
import com.kaisa.activity.bean.MoreTheme;
import com.kaisa.activity.constant.UrlHttp;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

public class MoreThemeActivity extends AppCompatActivity implements View.OnClickListener {
    @Bind(R.id.top_back)
    ImageView topBack;
    @Bind(R.id.photo_theme)
    GridView photoTheme;
    private MoreThemeAdapter adapter;
    private List<MoreTheme.MarketActivitysEntity> list = new ArrayList<MoreTheme.MarketActivitysEntity>();
    private HomeEntity homeEntity;
    private MoreTheme entitys;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_more_theme);
        ButterKnife.bind(this);
        Intent intent = getIntent();
        Bundle bundle = intent.getExtras();
        homeEntity = (HomeEntity) bundle.getSerializable("entity");
        initView();
        initData();
    }

    private void initData() {
        topBack.setOnClickListener(this);
        x.http().get(new RequestParams(UrlHttp.MORE_PATH_URL), new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String s) {
                for (int i = 0; i < 4; i++) {
                    MoreTheme.MarketActivitysEntity entity = new MoreTheme.MarketActivitysEntity();
                    entity.setActId(homeEntity.getMarketingActivities().getPosition1().get(i).getActId());
                    entity.setAddress(homeEntity.getMarketingActivities().getPosition1().get(i).getAddress());
                    entity.setBeginDate(homeEntity.getMarketingActivities().getPosition1().get(i).getBeginDate());
                    entity.setBottomColor(homeEntity.getMarketingActivities().getPosition1().get(i).getBottomColor());
                    entity.setEndDate(homeEntity.getMarketingActivities().getPosition1().get(i).getEndDate());
                    entity.setHeading(homeEntity.getMarketingActivities().getPosition1().get(i).getHeading());
                    entity.setLastModifyTime(homeEntity.getMarketingActivities().getPosition1().get(i).getLastModifyTime());
                    entity.setPic(homeEntity.getMarketingActivities().getPosition1().get(i).getPic());
                    entity.setReserved1(homeEntity.getMarketingActivities().getPosition1().get(i).getReserved1());
                    entity.setReserved2(homeEntity.getMarketingActivities().getPosition1().get(i).getReserved2());
                    entity.setShareContent(homeEntity.getMarketingActivities().getPosition1().get(i).getShareContent());
                    entity.setShareTitle(homeEntity.getMarketingActivities().getPosition1().get(i).getShareTitle());
                    entity.setSort(homeEntity.getMarketingActivities().getPosition1().get(i).getSort());
                    entity.setSubhead(homeEntity.getMarketingActivities().getPosition1().get(i).getSubhead());
                    list.add(entity);
                }
                 entitys = JSON.parseObject(s, MoreTheme.class);
                list.addAll(entitys.getMarketActivitys());
                adapter.notifyDataSetChanged();
            }

            @Override
            public void onError(Throwable throwable, boolean b) {

            }

            @Override
            public void onCancelled(CancelledException e) {

            }

            @Override
            public void onFinished() {

            }
        });
        photoTheme.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                switch (position){
                    case 0:
                        Intent intent = new Intent(MoreThemeActivity.this, EnjoyHolidayActivity.class);
                        startActivity(intent);
                        break;
                    case 1:
                        Intent shipintent = new Intent(MoreThemeActivity.this, ShipActivity.class);
                        startActivity(shipintent);
                        break;
                    case 2:
                        Intent intents = new Intent(MoreThemeActivity.this, LifeActivity.class);
                        intents.putExtra("title",list.get(2).getHeading());
                        intents.putExtra("url", list.get(2).getAddress());
                        startActivity(intents);
                        break;
                    case 3:
                        Intent intentss = new Intent(MoreThemeActivity.this, LifeActivity.class);
                        intentss.putExtra("title",list.get(3).getHeading());
                        intentss.putExtra("url", list.get(3).getAddress());
                        startActivity(intentss);
                        break;
                }
            }
        });
    }

    private void initView() {
        adapter = new MoreThemeAdapter(list, this);
        photoTheme.setAdapter(adapter);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.top_back:
                this.finish();
                break;
        }
    }
}
